package animalmanage;
import java.util.ArrayList;
import java.util.List;

public class AnimalArea implements ChangesAnimalInformation{
    private String AreaName;

    private AnimalHandlers AreaCorrespondingHandlers;

    private List<Animal> AnimalList;

    public AnimalArea() {
        this.AnimalList = new ArrayList<Animal>();
    }

    public AnimalArea(List<Animal> AnimalList){
        this.AnimalList = AnimalList;
    }

    public AnimalArea( AnimalHandlers handlers ){
        this.AreaCorrespondingHandlers = handlers;
        this.AnimalList = new ArrayList<Animal>();
    }

    public AnimalArea( List<Animal> AnimalList,AnimalHandlers handlers ){
        this.AnimalList = AnimalList;
        this.AreaCorrespondingHandlers = handlers;
    }
    //管理员权限
    @Override
    public boolean addAnimal(Animal animal) {
        //动物是否存在
        if(findAnimal(animal.getAnimalName())!=null){
            findAnimal(animal.getAnimalName()).setAnimalNum(findAnimal(animal.getAnimalName()).getAnimalNum()+animal.getAnimalNum());
            return true;
        }
        else
            return this.AnimalList.add(animal);
    }

    @Override
    public boolean deleteAnimal(Animal animal) {
        return this.AnimalList.remove(animal);
    }
    @Override
    public boolean deleteAnimal(String animalName) {
        if(findAnimal(animalName).getAnimalNum()>1){
            findAnimal(animalName).setAnimalNum(findAnimal(animalName).getAnimalNum()-1);
            return true;
        }
        else
            return this.AnimalList.remove(findAnimal(animalName));
    }
    @Override
    public boolean deleteAnimal(String animalName, int DeleteAnimalNum) {
        if(findAnimal(animalName)!=null&&(DeleteAnimalNum<findAnimal(animalName).getAnimalNum())){
            findAnimal(animalName).setAnimalNum(findAnimal(animalName).getAnimalNum()-DeleteAnimalNum);
            return true;
        }
        else
            return this.AnimalList.remove(findAnimal(animalName));
    }
    //修改动物信息
    @Override
    public boolean reviseAnimal(Animal oldAnimal, Animal newAnimal) {
        int oldAnimalIndex = 0;
        if(this.AnimalList.contains(oldAnimal)){
            oldAnimalIndex = this.AnimalList.indexOf(oldAnimal);
            this.AnimalList.set(oldAnimalIndex,newAnimal);
            System.out.println("Successfully revise");
            return true;
        }
        else{
            System.out.println("No find animal,Substitutions cannot be made");
        }
        return false;

    }
    @Override
    public Animal findAnimal(String AnimalName) {
        Animal foundAnimal=null;
        for( Animal animal : this.AnimalList ){
            if (animal.getAnimalName().equals(AnimalName)) {
                foundAnimal = animal;
            }
        }
        return foundAnimal;
    }
    @Override
    public String displayAllAnimal() {
        String displayAnimal = "";
        for( Animal animal : this.AnimalList ){
            displayAnimal+= animal.getAnimalName()+" "+ animal.getAnimalNum() +" ";
        }
        return displayAnimal;
    }
}